package com.coscon.controller;

import com.coscon.controller.base.BaseController;
import com.coscon.utils.DateUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * 截屏
 */
@Controller
@RequestMapping("/jm/manager/screenCapture")
public class ScreenCaptureController extends BaseController {


    @GetMapping("/index")
    public ModelAndView welcomePage(HttpServletRequest request){
        ModelAndView v = new ModelAndView("appPage/screenCapture");
        return v;
    }

    /**
     * 截屏
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws AWTException
     */
    @GetMapping(value = "/getScreenshotImg")
    public void getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException, AWTException {

        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/png");

        //截屏得到BufferedImage
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        //拷贝屏幕到一个BufferedImage对象screenshot
        BufferedImage screenshot = (new Robot()).createScreenCapture(new
                Rectangle(0, 0, width, height));
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(screenshot, "png", out);
        try {
            out.flush();
        } finally {
            out.close();
        }

    }



    //截屏并下载
    @GetMapping(value = "/getScreenshotImgAndDownLoad")
    public void getScreenshotImgAndDownLoad(HttpServletRequest request, HttpServletResponse response) throws IOException, AWTException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        //截屏得到BufferedImage
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        //拷贝屏幕到一个BufferedImage对象screenshot
        BufferedImage screenshot = (new Robot()).createScreenCapture(new
                Rectangle(0, 0, width, height));
        byte[] b = imageToBytes(screenshot);
        String fileName = DateUtils.getCurDateTime() +".png";
        response.reset();
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-disposition", "attachment; filename=\""
                +fileName  +"\"");
        response.setHeader("Content-Length", String.valueOf(b.length));
        IOUtils.write(b,response.getOutputStream());

    }


    /**
     * BufferedImage转byte[]
     *
     * @param bImage BufferedImage对象
     * @return byte[]
     * @auth zhy
     */
    private static byte[] imageToBytes(BufferedImage bImage) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "png", out);
        } catch (IOException e) {
            //log.error(e.getMessage());
        }
        return out.toByteArray();
    }

}
